package cn.xz.gobang.adapter

import android.graphics.drawable.Drawable
import android.view.ViewGroup
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import cn.xz.gobang.R
import cn.xz.gobang.bean.ChessPData
import cn.xz.gobang.util.AppHelper
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * 棋盘适配器*/
class ChessPAdapter : BaseQuickAdapter<ChessPData, BaseViewHolder>(R.layout.item_chess_p) {

    override fun convert(holder: BaseViewHolder, item: ChessPData) {
        //设置每格棋盘大小
        val mWindowW = AppHelper.getAppWidth()
        val mW1 = (mWindowW - 20)/15
        val mW2 = (mWindowW - 20)/15/2
        //给棋盘设置宽度
        val layoutParams = ConstraintLayout.LayoutParams(mW1, mW1)
        holder.itemView.layoutParams = layoutParams
        //外围棋盘设置背景
        val mP = holder.getView<ImageView>(R.id.iv_p)
        mP.scaleType = ImageView.ScaleType.FIT_XY
        val mPosition = holder.layoutPosition
        var mPD = AppHelper.getDrawable(R.mipmap.icon_f)
        if (mPosition == 0){
            val layoutParams = ConstraintLayout.LayoutParams(mW2, mW2)
            layoutParams.endToEnd = holder.itemView.id
            layoutParams.bottomToBottom = holder.itemView.id
            mP.layoutParams = layoutParams
            mPD = AppHelper.getDrawable(R.mipmap.icon_f_0)
        }else if (mPosition == 14){
            val layoutParams = ConstraintLayout.LayoutParams(mW2, mW2)
            layoutParams.startToStart = holder.itemView.id
            layoutParams.bottomToBottom = holder.itemView.id
            mP.layoutParams = layoutParams
            mPD = AppHelper.getDrawable(R.mipmap.icon_f_24)
        }else if (mPosition == 210){
            val layoutParams = ConstraintLayout.LayoutParams(mW2, mW2)
            layoutParams.topToTop = holder.itemView.id
            layoutParams.endToEnd = holder.itemView.id
            mP.layoutParams = layoutParams
            mPD = AppHelper.getDrawable(R.mipmap.icon_f_200)
        } else if (mPosition == 224){
            val layoutParams = ConstraintLayout.LayoutParams(mW2, mW2)
            layoutParams.topToTop = holder.itemView.id
            layoutParams.startToStart = holder.itemView.id
            mP.layoutParams = layoutParams
            mPD = AppHelper.getDrawable(R.mipmap.icon_f_256)
        }  else if (mPosition in 0..14){
            val layoutParams = ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mW2)
            layoutParams.startToStart = holder.itemView.id
            layoutParams.endToEnd = holder.itemView.id
            layoutParams.bottomToBottom = holder.itemView.id
            mP.layoutParams = layoutParams
            mPD = AppHelper.getDrawable(R.mipmap.icon_f_t)
        }else if (mPosition in 211..225){
            val layoutParams = ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mW2)
            layoutParams.startToStart = holder.itemView.id
            layoutParams.endToEnd = holder.itemView.id
            layoutParams.topToTop = holder.itemView.id
            mP.layoutParams = layoutParams
            mPD = AppHelper.getDrawable(R.mipmap.icon_f_b)
        }else if (mPosition % 15 == 0){
            val layoutParams = ConstraintLayout.LayoutParams(mW2, ViewGroup.LayoutParams.MATCH_PARENT)
            layoutParams.bottomToBottom = holder.itemView.id
            layoutParams.endToEnd = holder.itemView.id
            layoutParams.topToTop = holder.itemView.id
            mP.layoutParams = layoutParams
            mPD = AppHelper.getDrawable(R.mipmap.icon_f_l)
        }else if ((mPosition + 1) % 15 == 0){
            val layoutParams = ConstraintLayout.LayoutParams(mW2, ViewGroup.LayoutParams.MATCH_PARENT)
            layoutParams.bottomToBottom = holder.itemView.id
            layoutParams.startToStart = holder.itemView.id
            layoutParams.topToTop = holder.itemView.id
            mP.layoutParams = layoutParams
            mPD = AppHelper.getDrawable(R.mipmap.icon_f_r)
        }
        mP.setImageDrawable(mPD)



        //点击处理
        var mBG: Drawable? = null
        when(item.type){
            0->{
               mBG = AppHelper.getDrawable(R.drawable.icon_chess_0)
            }
            1->{
                mBG = AppHelper.getDrawable(R.drawable.icon_chess_1)
            }
            else->{

            }
        }
        holder.getView<ImageView>(R.id.iv_chess).setImageDrawable(mBG)

    }
}